var AssetModule = function () {
    var editable = function () {

        return {
            init: function () {
                var oTable = $('#modelEditable').dataTable({
                	"autoWidth":false,
                    "scrollX": true,
                	"lengthMenu": [
                                   [10, 15, 20, 50],
                                   [10, 15, 20, 50]
                               ],
                    "displayLength": 10,
                    "language": {
                        "lengthMenu": "每页 _MENU_ 条记录",
                        "paginate": {
                            "previous": "上一页",
                            "next": "下一页"
                        },
                        "info" : "从 _START_ 到  _END_ 条，总记录为 _TOTAL_ 条",    
                        "infoEmpty" : "记录数为0",
                     },
                     "columnDefs": [
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [0]
            						},
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [1]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [2]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [3]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [4]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [5]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [6]
            						}, 
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [7]
            						},
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [8]
            						},
            						{
            						    "data": null,
            						    'sortable': false,
            						    "defaultContent": "<center>--</center>",
            						    "targets": [9]
            						}
                                ],
                     "columns": [{ "data": "productCode" },
                                { "data": "briefName" },
                                { "data": "duration" },
                                { "data": "prospectiveEarnings"},
                                { "data": "incomeType" },
                                { "data": "scale" },
                                { "data": "state" },
                                { "data": "orders" },
                                { "data": "auditState" }],
                   "filter" : false,
                   "processing": false,
                   "serverSide": true,
                   "ajaxSource": ctx + "/assetAllocation/querylist",
                   "serverMethod": "post",
                   "fnRowCallback" : function(nRow, aData, displayIndex) {
                	   nRow.children[0].id = aData.id;
                	   $('td:eq(2)', nRow).html(aData.duration+"&nbsp;月");
                	   $('td:eq(5)', nRow).html("<span style=\"float:right\">"+formatCurrency(aData.scale)+"&nbsp;元"+"</span>");
                	   var incomeType;
                	   if(aData.incomeType == 0){
                		   incomeType = "固定";
                	   }else if(aData.incomeType == 1){
                		   incomeType = "浮动";
                	   }else if(aData.incomeType == 2){
                		   incomeType = "固定+浮动";
                	   }
                	   $('td:eq(4)', nRow).html(incomeType);
                	   $('td:eq(6)', nRow).html("<span style=\"float:right\">"+formatCurrency(aData.haveRaise)+"&nbsp;元"+"</span>");
                	   //$('td:eq(7)', nRow).html("待定");
                	   
                	   var state = aData.state;
                	   var state;
                	   //用户角色和产品状态,待确认
                	   if(aData.state == 1){
                		   state = "<center>正在募集中</center>";
                	   }else if(aData.state == 2){
                		   state = "<center>募集完成</center>";
                	   }
                	   var operation = "";
                       if(aData.operter!= null){
                       	operation = aData.operter;
                	   }
				
                	   $('td:eq(7)', nRow).html("<span style=\"float:right\">"+formatCurrency(aData.haveUse)+"&nbsp;元"+"</span>");
                	   $('td:eq(8)', nRow).html(state);
                	   $('td:eq(9)', nRow).html(operation);
                	   
                       return nRow;
                   },
                   "fnServerData" : function(ajaxSource, dataSet, fnCallback) {
                	   var json = "";
	                   var s;
	                   for (var i = 0; i < dataSet.length; i++) {
	                       s = dataSet[i];
	                   	   if (s.name == 'sEcho') {
	                   		   json = json + '"echo":' + s.value;
		                   } else if (s.name == 'iDisplayStart') {
		                	   json = json + ',"start":' + s.value;
		                   } else if (s.name == 'iDisplayLength') {
		                	   json = json + ',"pageSize":' + s.value;
		                   }
	                   }
	                   var form = $("form#queryForm").serializeJSON();
                   	   var js = form.replace('}', ',');
                   	   json = js + json + ',"orderBy":"create_time desc"' +"}";
                       $.ajax({
                       	"contentType":"application/json; charset=UTF-8",
                           "dataType" : 'json',
                           "type" : "POST",
                           "url" : ajaxSource,
                           "data" : json,
                           "success" : fnCallback
                       });
                   },
                });
              
                // 确认募集完成
                $('#modelEditable').on("click", "button.confirmBtn", (function (e) {
                	e.preventDefault();//去除事件本身动作
                	var nRow = $(this).parents('tr')[0];
                    var id = nRow.children[0].id;
                    $.ajax({
                        type: "post",
                        url: ctx + "/assetAllocation/raiseFinish",
                        data:"id="+id,
                        dataType: "json",
                        success: function(msg) {
                            if (msg.code == 1) {
                                bootbox.alert("确认募集完成！");
                                oTable.fnDraw(false);
                            } else {
                                bootbox.alert(msg.message);
                            }
                        },
                        error: function(msg) {
                            bootbox.alert(msg.message);
                        }
                    });
                }));
                
                // 历史资产配置
                $('#modelEditable').on("click", "button.historyBtn", (function (e) {
                	e.preventDefault();
                	
                	var nRow = $(this).parents('tr')[0];
                    var id = nRow.children[0].id;
                    window.location.href = ctx + '/assetAllocation/history/'+ id;
                }));
                
                // 发起资产配置
                $('#modelEditable').on("click", "button.startBtn", (function (e) {
                	e.preventDefault();
                	
                    var nRow = $(this).parents('tr')[0];
                    var id = nRow.children[0].id;
                    window.location.href = ctx + '/assetAllocation/add/'+ id;
                }));
                
                //查询
                $('#queryBtn').click(function(){
                	oTable.fnClearTable();
                });
            }
        };

    }();
    
    return {
        init: function () {
        	editable.init();
        }
    };
}();